package com.ovopark.firecar.utils;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.text.TextUtils;


import java.util.List;

/**
 * 厂商通道
 * app内推送点击处理
 */
public class PushUtils {

    private static final String TAG = "PushUtils";

    public static void checkPushContent(Context context, String pushContent) {

    }



    public static String getMetaValue(Context context, String metaKey) {
        Bundle metaData = null;
        String apiKey = null;
        if (context == null || metaKey == null) {
            return null;
        }
        try {
            ApplicationInfo ai = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
            if (null != ai) {
                metaData = ai.metaData;
            }
            if (null != metaData) {
                apiKey = metaData.getString(metaKey);
            }
        } catch (NameNotFoundException e) {

        }
        return apiKey;
    }

    public static boolean isAppRunning(Context context) {
        boolean isRunning = false;
        if (context == null) {
            return false;
        }
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningTaskInfo> tasksInfo = manager.getRunningTasks(100);
        if (tasksInfo.size() > 0) {
            for (RunningTaskInfo info : tasksInfo) {
                if (info.topActivity.getPackageName().equals(context.getPackageName()) &&
                        info.baseActivity.getPackageName().equals(context.getPackageName())) {
                    isRunning = true;
                    break;
                }
            }
        }
        return isRunning;
    }

    public static boolean isRunning(Context mContext) {

        boolean isRun = false;
        ActivityManager activityManager = (ActivityManager) mContext
                .getSystemService(Context.ACTIVITY_SERVICE);
        // 获取所有正在运行的app
        List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager
                .getRunningAppProcesses();
        // 遍历app，获取应用名称或者包名
        for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
            if (appProcess.processName.equals(mContext.getPackageName()) && appProcesses.size() > 2) {
                isRun = true;
            }
        }
        return isRun;
    }

    /**
     * 判断是否在后台运行
     *
     * @param context
     * @return
     */
    public static boolean isRunningBackground(Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
        String currentPackageName = cn.getPackageName();
        return !(!TextUtils.isEmpty(currentPackageName) && currentPackageName.equals(context.getPackageName()));
    }

    //判断当前界面显示的是哪个Activity
    public static String getTopActivity(Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);
        ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
        return cn.getClassName();
    }


}
